package com.example.JunitTest;

import org.springframework.util.FileCopyUtils;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CreateExcel {
    public static void main(String[] args) throws IOException {
        // 模板文件路径
        String templateFilePath = "";
        // 周报日期格式
        String dateFormat = "M月dd日";
        int year = 2022;
        int month = 1;
        int dayOfMonth = 1;
        createExcel(year, month, dayOfMonth, templateFilePath, dateFormat);
    }

    /**
     * 创建周报
     * @param year 开始年份
     * @param month 开始月份  (the month numbering is <em>0-based</em>).
     * @param dayOfMonth 开始日期
     * @param dateFormat 日期格式
     * @throws IOException 文件传输异常
     */
    private static void createExcel(int year, int month, int dayOfMonth, String templateFilePath, String dateFormat) throws IOException {
        Calendar firstDate = new Calendar.Builder().setCalendarType("gregorian").setDate(year, month, dayOfMonth).build();
        Date firstDateTime;
        Date secondDateTime;
        String firstDateStr;
        String secondDateStr;
        String s;

        for (int i = 0; i < 45; i++) {
            firstDateTime = firstDate.getTime();
            firstDate.add(Calendar.DAY_OF_MONTH, 4);
            secondDateTime = firstDate.getTime();
            firstDateStr = new SimpleDateFormat(dateFormat).format(firstDateTime);
            secondDateStr = new SimpleDateFormat(dateFormat).format(secondDateTime);
            s = firstDateStr + "-" + secondDateStr;
            firstDate.add(Calendar.DAY_OF_MONTH, 3);

            File outFile = new File(templateFilePath.replace("模板", s));
            File inFile = new File(templateFilePath);

            System.out.println(outFile.getName());
            FileCopyUtils.copy(inFile, outFile);
        }
    }
}
